package com.s7.statusbar;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.IdRes;
import androidx.core.view.ViewCompat;

import com.google.android.material.appbar.CollapsingToolbarLayout;

/**
 * 状态栏管理工具
 */
public class StatusBarManager {

    /**
     * 设置Toolbar的高度加上状态栏高度
     * @param window
     * @param toolbar
     */
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static void setToolbar(Window window, View toolbar) {
        if (toolbar.getTag() == null) {
            CollapsingToolbarLayout.LayoutParams lp = (CollapsingToolbarLayout.LayoutParams) toolbar.getLayoutParams();
            int statusBarHeight = StatusBarTools.getStatusBarHeight(window.getContext());
            lp.height += statusBarHeight;
            toolbar.setLayoutParams(lp);
            toolbar.setPadding(toolbar.getPaddingLeft(), toolbar.getPaddingTop() + statusBarHeight,
                    toolbar.getPaddingRight(), toolbar.getPaddingBottom());
            toolbar.setTag(true);
        }
    }

    /**
     * 设置是否保留状态栏空间（布局是否沉浸到状态栏）
     * @param window
     * @param fitSystemWindows
     */
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public static void internalSetFitsSystemWindows(Window window, boolean fitSystemWindows) {
        ViewGroup contentView = window.findViewById(Window.ID_ANDROID_CONTENT);
        View childView = contentView.getChildAt(0);
        if (childView != null) {
            //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View .
            // 预留出系统 View 的空间.
            childView.setFitsSystemWindows(fitSystemWindows);
            ViewCompat.requestApplyInsets(childView);
        }
    }

    /**
     * 解决状态栏与标题栏之间有一个与状态栏同样大小的间距
     * @param activity
     */
    public static void resetActionBarContainerTopMargin(Activity activity) {
        resetActionBarContainerTopMargin(activity.getWindow());
    }

    /**
     * 解决状态栏与标题栏之间有一个与状态栏同样大小的间距
     * @param window
     */
    public static void resetActionBarContainerTopMargin(Window window) {
        View contentView = window.findViewById(android.R.id.content);
        ViewGroup group = (ViewGroup) contentView.getParent();
        if (group.getChildCount() > 1) {
            View view = group.getChildAt(1);
            internalResetActionBarContainer(view);
        }
    }

    /**
     * 解决状态栏与标题栏之间有一个与状态栏同样大小的间距
     * @param activity               The window of the current activity.
     * @param actionBarContainerId android.support.v7.appcompat.R.id.action_bar_container
     */
    public static void resetActionBarContainerTopMargin(Activity activity, @IdRes int actionBarContainerId) {
        resetActionBarContainerTopMargin(activity.getWindow(), actionBarContainerId);
    }

    /**
     * 解决状态栏与标题栏之间有一个与状态栏同样大小的间距
     * @param window               The window of the current activity.
     * @param actionBarContainerId android.support.v7.appcompat.R.id.action_bar_container
     */
    public static void resetActionBarContainerTopMargin(Window window, @IdRes int actionBarContainerId) {
        View view = window.findViewById(actionBarContainerId);
        internalResetActionBarContainer(view);
    }

    /**
     * 解决状态栏与标题栏之间有一个与状态栏同样大小的间距
     * @param actionBar
     */
    public static void internalResetActionBarContainer(View actionBar) {
        if (actionBar != null) {
            ViewGroup.LayoutParams params = actionBar.getLayoutParams();
            if (params instanceof ViewGroup.MarginLayoutParams) {
                ((ViewGroup.MarginLayoutParams) params).topMargin = 0;
                actionBar.setLayoutParams(params);
            }
        }
    }
}
